.pc = $801 "BASIC Entry"
    // This corresponds to the BASIC statement "1 SYS 2062"
    .byte 12, 8, 1, 0, 158, 32, 50, 48, 54, 50, 0, 0, 0

    // and this is address 2062
    jmp main_entry

// import other code
.import source "util.asm"
.import source "sprites.asm"
.import source "keyboard.asm"
.import source "hero.asm"
.import source "background.asm"
.import source "level.asm"
.import source "hud.asm"
.import source "music.asm"
.import source "global_strings.asm"

.pc = * "Main Routines"

main_entry:
{
    // disable standard interrupt
    lda #1
    sta $dc0d
    
    // save stack base pointer
    tsx
    stx stack_base
    
    jsr init_zero_page
    
    // load data files
    :LoadFile("SPRITEDATA", $c400)
    :LoadFile("TILES", tile_data)

    // load start level
    :LoadFile("START-LEVEL", $8000)

    jsr init_background
    
    // set sprite multicolor registers
    lda #1
    sta $d026
    lda #0
    sta $d025

    jsr init_hero
    jsr init_music
    jsr hud_init

    jsr map_enter

    jmp main_loop
}

//////////////////////////////

main_loop:
{
    // turn on background at raster line 251
    :SetBorder(0)   
    :RasterWait(251)
    jsr background_enable

    :SetBorder(1)
    jsr scan_keyboard

    :SetBorder(2)    
    jsr update_hero

    :SetBorder(3)
    jsr level_check_triggers

    // turn on hud at raster line 234
    :SetBorder(0)
    :RasterWait(234)
    :Nops(12)
    jsr hud_enable

    // exit on 0 key pressed
    :Test0Key()
    beq main_exit

    jmp main_loop
}

//////////////////////////////
    
main_exit:
{
    // wait for 0 to be released
wait:
    jsr scan_keyboard
    :Test0Key()
    beq wait
    
    jsr destroy_hero

    // clear keyboard buffer
    lda #0
    sta 198

    jsr restore_zero_page

    // select bank 0 ($0000-$3fff) for graphics
    lda $dd00
    ora #3
    sta $dd00

    // reenable standard interrupt
    lda #129
    sta $dc0d

    // restore graphics settings, clear screen
    jsr $ff81

    // return control to BASIC
    rts
}
